package com.ruitu.mall_98_2.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.CalcUtils;
import com.beanu.arad.utils.ClipboardUtils;
import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.TimeUtils;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.dialog.CustomAlertDialog;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.mvp.contract.WaitingPayOrderDetailsContract;
import com.ruitu.mall_98_2.mvp.model.WaitingPayOrderDetailsModel;
import com.ruitu.mall_98_2.mvp.presenter.WaitingPayOrderDetailsPresenter;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.bean.order.ConfirOrder;
import com.ruitu.router_module.bean.order.Order;
import com.ruitu.router_module.bean.order.OrderList;
import com.ruitu.router_module.bean.product.Product;

import java.text.SimpleDateFormat;

public class WaitingPayOrderDetailsActivity extends MyBaseActivity<WaitingPayOrderDetailsPresenter, WaitingPayOrderDetailsModel> implements WaitingPayOrderDetailsContract.View {
    private OrderList orderList;

    private TextView tv_waiting_pay_commodity_total;//商品
    private TextView tv_waiting_pay_expressage_total;//运费
    private TextView tv_waiting_pay_order_total;//订单的金额
    private RelativeLayout rl_coupon;
    private TextView tv_cut;//满减优惠券
    private TextView tv_waiting_pay_actual_money;//需付款
    private TextView tv_order_number;//订单编号
    private TextView tv_copy;//复制
    private TextView tv_creat_time;//订单创建时间
    private TextView tv_waiting_pay_pay_order;//去付款
    private TextView tv_waiting_pay_cancel_order;//取消订单

    private TextView tv_consignee; //姓名
    private TextView tv_tel;//手机号
    private TextView tv_address;//地址

    private TextView tv_order_price_type;
    private TextView tv_pay_type;
    private TextView tv_order_status;
    private TextView tv_advance_ratio;//预付比例
    private TextView tv_advance;//定金
    private TextView tv_tail_money;//尾款
    private TextView tv_send_time;//预计发货时间

    private RecyclerView recyclerView;
    //    private List<OrderDetail> orderDetails = new ArrayList<>();
    private DataAdapter adapter;
    private CustomAlertDialog customAlertDialog;
    private LinearLayout ll_waiting_pay;

    private LinearLayout ll_advance;//预订
    private LinearLayout ll_waiting_message;//等候通知


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_waiting_pay_pay_order) {
            ConfirOrder confirOrder = new ConfirOrder();
            confirOrder.setId(orderList.getId());
            confirOrder.setCollectGoodsAddress(orderList.getCollectGoodsAddress());
            confirOrder.setCollectGoodsPhone(orderList.getCollectGoodsPhone());
            confirOrder.setConsigneeName(orderList.getConsigneeName());
            confirOrder.setOrderProvinceCode(orderList.getOrderProvinceCode());
            confirOrder.setOrderCityCode(orderList.getOrderCountyCode());
            confirOrder.setOrderCountyCode(orderList.getOrderCountyCode());
            confirOrder.setOrderTotal(orderList.getOrderTotal());
            confirOrder.setOrderType(orderList.getOrderType());
            confirOrder.setIsPayOver(orderList.getIsPayOver());
            Intent intent = new Intent(this, PayOrderActivity.class);
            intent.putExtra("confirOrder", confirOrder);
            startActivity(intent);
        }

        if (v == tv_waiting_pay_cancel_order) {
            customAlertDialog = new CustomAlertDialog(this, "确定取消订单吗?", new CustomAlertDialog.OkOnClickListener() {
                @Override
                public void OkOnClick() {
                    showProgress();
                    mPresenter.cancelOrder(orderList.getId());
                    customAlertDialog.dismiss();
                }
            });
            customAlertDialog.show();
        }

        if (v == tv_copy) {
            ClipboardUtils.copyText(tv_order_number.getText());
            ToastUtils.showShort("已复制到粘贴板");
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_waiting_order_details;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setHeadTitle("订单详情");
        orderList = (OrderList) getIntent().getSerializableExtra("orderList");
        tv_waiting_pay_commodity_total = findViewById(R.id.tv_waiting_pay_commodity_total);
        tv_waiting_pay_expressage_total = findViewById(R.id.tv_waiting_pay_expressage_total);
        tv_waiting_pay_order_total = findViewById(R.id.tv_waiting_pay_order_total);
        tv_waiting_pay_actual_money = findViewById(R.id.tv_waiting_pay_actual_money);
        tv_waiting_pay_pay_order = findViewById(R.id.tv_waiting_pay_pay_order);
        tv_waiting_pay_cancel_order = findViewById(R.id.tv_waiting_pay_cancel_order);
        ll_waiting_pay = findViewById(R.id.ll_waiting_pay);
        tv_order_number = findViewById(R.id.tv_order_number);
        tv_creat_time = findViewById(R.id.tv_creat_time);
        tv_copy = findViewById(R.id.tv_copy);
        tv_cut = findViewById(R.id.tv_cut);
        rl_coupon = findViewById(R.id.rl_coupon);
        tv_consignee = findViewById(R.id.tv_consignee);
        tv_tel = findViewById(R.id.tv_tel);
        tv_address = findViewById(R.id.tv_address);
        tv_advance_ratio = findViewById(R.id.tv_advance_ratio);
        recyclerView = findViewById(R.id.recyclerView);
        tv_pay_type = findViewById(R.id.tv_pay_type);
        tv_order_price_type = findViewById(R.id.tv_order_price_type);
        tv_tail_money = findViewById(R.id.tv_tail_money);
        tv_order_status = findViewById(R.id.tv_order_status);
        tv_advance = findViewById(R.id.tv_advance);
        tv_send_time = findViewById(R.id.tv_send_time);
        ll_advance = findViewById(R.id.ll_advance);
        ll_waiting_message = findViewById(R.id.ll_waiting_message);
        tv_copy.setOnClickListener(this);
        tv_waiting_pay_pay_order.setOnClickListener(this);
        tv_waiting_pay_cancel_order.setOnClickListener(this);
        initData();
    }

    private void initData() {
        tv_consignee.setText(orderList.getConsigneeName());
        tv_address.setText(orderList.getOrderProvinceCode() + orderList.getOrderCityCode() + orderList.getOrderCountyCode() + orderList.getCollectGoodsAddress());
        tv_tel.setText(orderList.getCollectGoodsPhone());
        double expressage_total = 0;
        for (int i = 0; i < orderList.getOrderDetails().size(); i++) {
            OrderList.OrderDetailsBean orderDetailsBean = orderList.getOrderDetails().get(i);
            expressage_total = CalcUtils.add(expressage_total, orderDetailsBean.getFreight());
        }
        tv_cut.setText("￥" + orderList.getDiscountTotal());
        if (TextUtils.equals(orderList.getOrderType(), "1") || TextUtils.equals(orderList.getOrderType(), "2")) {
            ll_advance.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(orderList.getPreSaleDays())) {
                tv_send_time.setText("付款后" + orderList.getPreSaleDays() + "天");
            }

            if (!TextUtils.isEmpty(orderList.getBookingDeliveryTime())) {
                tv_send_time.setText(TimeUtils.millis2String(Long.parseLong(orderList.getBookingDeliveryTime()),new SimpleDateFormat("yyyy-MM-dd")));
            }

            tv_advance_ratio.setText(orderList.getAdvanceRatio() + "%");
            tv_advance.setText("￥" + orderList.getAdvanceMoney());
            tv_tail_money.setText("￥" + orderList.getTailMoney());
            if (TextUtils.equals(orderList.getIsPayOver(), "0")) {
                tv_pay_type.setText("需付订金");
                tv_waiting_pay_pay_order.setText("支付订金");
                tv_order_status.setText("待支付");
                tv_waiting_pay_actual_money.setText("￥" + orderList.getAdvanceMoney());
            } else if (TextUtils.equals(orderList.getIsPayOver(), "2")) {
                tv_pay_type.setText("已付订金");
                ll_waiting_pay.setVisibility(View.GONE);
                tv_order_status.setText("制作中");
                tv_waiting_pay_actual_money.setText("￥" + orderList.getAdvanceMoney());
                ll_waiting_message.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals(orderList.getIsPayOver(), "4")) {
                tv_pay_type.setText("需付尾款");
                tv_waiting_pay_cancel_order.setVisibility(View.GONE);
                tv_waiting_pay_pay_order.setText("支付尾款");
                tv_order_status.setText("待付尾款");
            } else {
                tv_order_status.setText("未支付");
            }

        } else {
            tv_pay_type.setText("付款金额");
            tv_waiting_pay_actual_money.setText("￥" + orderList.getOrderTotal());
            tv_order_status.setText("未支付");
        }
        tv_waiting_pay_order_total.setText("￥" + orderList.getOrderTotal());
        tv_waiting_pay_commodity_total.setText("￥" + CalcUtils.sub(CalcUtils.add(orderList.getOrderTotal(), orderList.getDiscountTotal()), expressage_total));
        tv_waiting_pay_expressage_total.setText("￥" + expressage_total);
        tv_creat_time.setText(TimeUtils.millis2String(orderList.getCreateTime()));
        tv_order_number.setText(orderList.getId());
        //TODO 滑动监听问题待解决
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        adapter = new DataAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.setDataList(orderList.getOrderDetails());
    }

    @Override
    public void setSuc() {
        finish();
    }

    class DataAdapter extends ListBaseAdapter<OrderList.OrderDetailsBean> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_data_waiting_pay_commodity;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_commodity_info = holder.getView(R.id.tv_commodity_info);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            TextView tv_commodity_num = holder.getView(R.id.tv_commodity_num);
            LinearLayout ll_commmodity_item = holder.getView(R.id.ll_commmodity_item);
            LinearLayout ll_xiajia = holder.getView(R.id.ll_xiajia);
            final OrderList.OrderDetailsBean orderDetailsBean = mDataList.get(position);
            Glide.with(mContext).load(Config.BASE_URL + orderDetailsBean.getGoodsUrl()).apply(Arad.opts).into(iv_commodity);
            tv_commodity_name.setText(orderDetailsBean.getGoodsName());
            tv_commodity_info.setText(orderDetailsBean.getSkuContent());
            tv_commodity_price.setText("￥" + orderDetailsBean.getGoodsPrice());
            tv_commodity_num.setText("x" + orderDetailsBean.getQuantity());

            ll_commmodity_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    Product product = new Product();
                    product.setGoodId(orderDetailsBean.getGoodsId());
                    bundle.putSerializable("product", product);
                    try {
                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ProductDetailActivity", bundle);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });

            if (TextUtils.equals(orderDetailsBean.getGoodStatus(),"0")){
                ll_xiajia.setVisibility(View.VISIBLE);
            }else if (TextUtils.equals(orderDetailsBean.getGoodStatus(),"1")){
                ll_xiajia.setVisibility(View.GONE);
            }

        }
    }
}
